package cs2340.spring2012.droidception.toDo;

import java.io.*;
import java.util.*;

public class Files {
	
	private static String currentUserID;
	private static UserAccount currentUser;
	/**
	 * Verifies the login information entered by the user.
	 * If the login information is accurate, this method then
	 * loads the user's information and sets the current user.
	 * 
	 * Preconditions: Username/password not null, username
	 * contains no illegal characters
	 * 
	 * Postconditions: If login information is correct, the
	 * current user will be set. Else, nothing happens.
	 * 
	 * @param username The username the user enters to login
	 * @param password	The password the user enters to login
	 * @return True if the login was successful
	 */
	public static boolean verifyUserAccount(String username, String password) {
		Scanner userFileScanner = new Scanner("");
		// Try to find the file corresponding to the given username
		try {
			File userFile = new File("./userAccounts/" + username + ".info");
			userFileScanner = new Scanner(userFile);
		}
		// If the file is not found, that username does not exist.
		catch(FileNotFoundException e){
			return false;
		}
		
		userFileScanner.useDelimiter("\n");
		String storedPassword = userFileScanner.next();
		
		// If the file is found and the password is correct,
		// the user account is set (the user is logged in).
		if (password.equals(storedPassword)) {
			
			currentUserID = username;
			
			String name = userFileScanner.next();
			String email = userFileScanner.next();
			
			// Parses through comma-separated list
			// of categories
			String categoryNames = userFileScanner.next();
			Scanner categoryScanner = new Scanner(categoryNames);
			categoryScanner.useDelimiter(", ");
			
			Category categoryToAdd;
			String categoryName;
			ArrayList<Category> categories = new ArrayList<Category>();
			
			while (categoryScanner.hasNext()) {
				categoryName = categoryScanner.next();
				categoryToAdd = new Category(categoryName);
				categories.add(categoryToAdd);
			}
			
			// Sets the current user's account information
			currentUser = new UserAccount(name, username, email, password);
			currentUser.setCategories(categories);
			setItems();
			return true;
		}
		// If the password is incorrect, the user is not logged in.
		else {
			return false;
		}
	}
	/**
	 * Scans through the current user's items file 
	 * Preconditions: user must be logged in
	 * Postconditions: current user has all saved to-do items
	 */
	private static void setItems() {
		
	}
	public static UserAccount getUserAccount() {
		return currentUser;
	}
	/**
	 * Updates a user's account information file.
	 * 
	 * @param updatedUser
	 */
	public static void rewriteUserAccount(UserAccount updatedUser) {
		
	}
	/**
	 * Creates a new user account information file and a new
	 * items file. Calls rewriteUserAccount
	 * @param newUser
	 * @return
	 */
	public static boolean writeUserAccount(UserAccount newUser) {
		return false;
	}
	
	public static boolean changePassword(String newPassword) {
		return false;
	}
	/*
	public static void rewriteUserItems(ArrayList<ToDoItem> items) {
		
	}
	*/
	
}